//
//  ServiceDetailViewController.m
//  DaDaLiCai
//
//  Created by apple on 15/11/24.
//  Copyright © 2015年 Mikeshihua. All rights reserved.
//

#import "ServiceDetailViewController.h"

@interface ServiceDetailViewController ()
@property (nonatomic,copy)NSString * groupId;
@property (nonatomic,strong)TopView * topView;
@property (nonatomic,strong)UIWebView  * detailView;

@property (nonatomic,strong)UIView *noDataView;
@property (nonatomic,strong)UIView *noNet;
@end

@implementation ServiceDetailViewController
-(id)initWithGroupId:(NSString *)groupId
{
    if (self = [super init])
    {
        _groupId = groupId;
    }
    return self;
}
-(void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    [_detailView removeFromSuperview];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.view addSubview:self.topView];
    [self.view addSubview:self.detailView];
    [self.view addSubview:self.noDataView];
    [self.view addSubview:self.noNet];
    [self refresh];
}
-(TopView *)topView
{
    if (!_topView)
    {
        _topView = [[TopView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64)];
        
        UIButton * backBtn = [[UIButton alloc]initWithFrame:CGRectMake(10.0f, 35.0f, 49.0f, 16.0f)];
        [backBtn setImage:[UIImage imageNamed:@"icon_back"] forState:UIControlStateNormal];
        [backBtn addTarget:self action:@selector(backBtnCLick:) forControlEvents:UIControlEventTouchUpInside];
        backBtn.titleLabel.textAlignment = NSTextAlignmentRight;
        [_topView addSubview:backBtn];
    }
    return _topView;
}
-(void)refresh
{
    _noNet.hidden = TRUE;
    _noDataView.hidden = TRUE;
    [[AnimationIndicator sharedIndicator] startAnimation];
    
    [AppGlobalClass PostDataWithNSstring:GETGROUPLISTDETAIL withDictionary:@{@"groupId":_groupId} success:^(NSDictionary *dictionary) {
        if([[dictionary objectForKey:@"success"] isEqualToNumber:[NSNumber numberWithBool:TRUE]])
        {
            NSArray * arr = [[[dictionary objectForKey:@"responseData"]firstObject]objectForKey:@"rows"];
            NSDictionary * dict = [arr firstObject];
            [_topView setTitile:[dict objectForKey:@"name"]];
            [_detailView loadHTMLString:[dict objectForKey:@"introduce"] baseURL:nil];
        }else
        {
            NSString * message = [dictionary objectForKey:@"message"];
            ALERT_VIEW(message);
            _noDataView.hidden = FALSE;
        }
        [[AnimationIndicator sharedIndicator] stopAnimationWithLoadText:@"" withType:TRUE];
    } failure:^(NSError *error) {
        _noDataView.hidden = FALSE;
        [[AnimationIndicator sharedIndicator] stopAnimationWithLoadText:@"" withType:TRUE];
    } networkerror:^(NSString *networkerror) {
        _noNet.hidden = FALSE;
        [[AnimationIndicator sharedIndicator] stopAnimationWithLoadText:@"" withType:TRUE];
    }];
}
-(UIWebView *)detailView
{
    if (!_detailView)
    {
        _detailView = [[UIWebView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64)];
    }
    return _detailView;
}
//没网提示
#pragma mark - 无网提示
-(UIView *)noNet
{
    if (!_noNet) {
        _noNet = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 200)];
        _noNet.center = CGPointMake(self.view.center.x, self.view.center.y - 64 - 32);
        _noNet.hidden = TRUE;
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(0, 0, 200, 200);
        [button setImage:[UIImage imageNamed:@"img_nonetwork"] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(noResultButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 200 - 40, 200, 44)];
        label.text = @"网络不通\n点击重新搜索";
        label.font = [UIFont systemFontOfSize:15.0f];
        label.textAlignment = NSTextAlignmentCenter;
        label.numberOfLines = 0;
        label.textColor = [UIColor colorWithWhite:0.000 alpha:0.540];
        [_noNet addSubview:label];
        [_noNet addSubview:button];
    }
    return _noNet;
}

-(UIView *)noDataView{
    if (!_noDataView) {
        
        _noDataView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 200, 200)];
        _noDataView.center =CGPointMake(self.view.center.x, self.view.center.y - 64 - 32);
        _noDataView.hidden = TRUE;
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(0, 0, 200, 200);
        [button setImage:[UIImage imageNamed:@"img_nodata"] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(noResultButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 200 - 40, 200, 44)];
        label.text = @"暂无数据\n点击重新搜索";
        label.textAlignment = NSTextAlignmentCenter;
        label.numberOfLines = 0;
        label.font = [UIFont systemFontOfSize:14.0f];
        
        label.textColor = [UIColor colorWithWhite:0.000 alpha:0.540];
        [_noDataView addSubview:label];
        [_noDataView addSubview:button];
    }
    
    return _noDataView;
}
- (void)noResultButtonAction:(id)sender
{
    [self refresh];
}
- (void)backBtnCLick:(UIButton*)btn
{
    [[AnimationIndicator sharedIndicator] stopAnimationWithLoadText:@"" withType:TRUE];
    [self.navigationController popViewControllerAnimated:TRUE];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
